package zlx.springboot.jsp.controller;

import com.sun.org.apache.regexp.internal.RE;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.RequestPath;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@Controller
public class SuccessController {
    //获取日记
    private static final Logger LOGGER = LoggerFactory.getLogger(SuccessController.class);

    @GetMapping(value = "/abc")
    public String success(Model model){
        model.addAttribute("success","你好啊，大熊得");
        return "/success";
    }
    //文件上传模板
    @PostMapping(value = "/uploadFile")
    @ResponseBody
    public String uploadFile(@RequestParam("file") MultipartFile file, Model model){
        if (file.isEmpty()){
            LOGGER.info("文件为空，无法上传！");
            return "文件为空无法上传";
        }
        String fileName = file.getOriginalFilename();
        String filePath = "D:/Idea/jsp/src/main/resources/file/";

        File uploadFile = new File(filePath+fileName);
        try {
            file.transferTo(uploadFile);
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.getMessage(),e);
            e.printStackTrace();
        }
        return "上传文件失败";
    }
}
